from src.db.db_models import Stu_Info
from src.models import check_models


async def add_stu(student):
    stu = await Stu_Info.create(stu_id=student.stu_id, stu_class_name=student.stu_class_name,
                                stu_name=student.stu_name, stu_jiguan=student.stu_jiguan,
                                stu_university=student.stu_university, stu_major=student.stu_major,
                                stu_start_time=student.stu_start_time, stu_end_time=student.stu_end_time,
                                stu_major_level=student.stu_major_level, stu_hr_id=student.stu_hr_id,
                                stu_age=student.stu_age, stu_gender=student.stu_gender)

    return {f"添加学生{stu.stu_name}成功"}


async def find_student(stu_id,
                       stu_class_name,
                       stu_name
                       ):
    conditions = {}
    try:
        if stu_id is not None:
            conditions['stu_id'] = stu_id
        if stu_class_name is not None:
            conditions['stu_class_name'] = stu_class_name
        if stu_name is not None:
            conditions['stu_name'] = stu_name
    except Exception as e:
        return {"警告": "输入有误，请重新输入"}
    students = await Stu_Info.filter(**conditions)
    if students:
        return students
    else:
        return {"警告": "您输入的参数有误，请重新输入"}


async def update_student(stu_id, student):
    data = student.dict(exclude_unset=True)
    data.pop('stu_id')
    await Stu_Info.filter(stu_id=stu_id).update(**data)
    return {f"学生{stu_id}更新成功"}
